home *** CD-ROM | disk | FTP | other *** search
- /*
- MINIMUM/MAXIMUM OF INTEGER LISTS IN TURBO PROLOG
- BY GARRY J. VASS [72301,3311]
-
- */
- nowarnings
-
- domains
- list = integer*
- predicates
- integer_list_maximum(integer, list,integer)
- integer_list_minimum(integer, list, integer)
- demonstrate
- any_list(list)
- clauses
- any_list([-6,-1,-2,-88,-2,200,-3,-4,-5]).
-
-
- /* if the list is empty, then the highest value */
- /* found thus far (in "X") must be the maximum. */
- /* Set "M" to the maximum because "X" will get */
- /* reassigned. */
-
- integer_list_maximum(X, [], M) if M = X.
-
- /* if the head of the list is greater than */
- /* the highest value encountered so far then*/
- /* compare the head to the rest of the list */
-
- integer_list_maximum(X, [H|T],M) if
- H >= X and
- integer_list_maximum(H,T,M).
-
- /* if the above two rules don't work, then */
- /* continue the tail recursion process. */
-
- integer_list_maximum(X,[H|T],M) if
- integer_list_maximum(X,T,M).
-
-
-
- /* same logic applies to min predicate */
-
- integer_list_minimum(X,[],M) if M = X.
- integer_list_minimum(X, [H|T],M) if
- H <= X and
- integer_list_minimum(H,T,M).
- integer_list_minimum(X, [H|T],M) if
- integer_list_minimum(X,T,M).
-
- demonstrate if
- any_list([H|T]) and
- integer_list_maximum(H,[H|T],Maximum) and
- write("Maximum = ",Maximum) and nl and
- integer_list_minimum(H,[H|T],Minimum) and
- write("Minimum = ",Minimum) and nl.
-
- /* first variable is initialized to head */
- /* of list to avoid hard-coding an initial */
- /* value. */
- /*
- goal
- demonstrate
- */
- G-²
- ü##á$8>┌/╛÷:╛í
- üâ╘à[┌ »Pi┴k▌εV%ò0ZPEεΣs|Zgrx1ìLU╗Æ┬«▄╔0
- @fü¬╚c╛)N(2ñ└Σ$Ç HYMV₧░y